﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ESMOffline
{
    public partial class frmLecturaContextoEE : Form
    {

        #region Propiedades Publicas y Privadas

        ESM.Objetos.CLecturaContextoEE objCLecturaContextoEE = new ESM.Objetos.CLecturaContextoEE();
        int lceeidmedicion = 0;

        int _eeid = 0;
        int _medid = 0;
        int lceeid = 0;

        #endregion

        public frmLecturaContextoEE()
        {
            InitializeComponent();
        }

        public frmLecturaContextoEE(int eeid, int medid)
        {
            InitializeComponent();

            _eeid = eeid;
            _medid = medid;

            CargarLCEE(_medid);
        }

        protected void Almacenar(int idee, int idmed)
        {
            #region Asignacion

            objCLecturaContextoEE.Numero_Sedes = Convert.ToInt32(txtSedes.Text);

            if (cblistjornadas1.Checked)
                objCLecturaContextoEE.F11 = true;
            if (cblistjornadas2.Checked)
                objCLecturaContextoEE.F12 = true;
            if (cblistjornadas3.Checked)
                objCLecturaContextoEE.F13 = true;
            if (cblistjornadas4.Checked)
                objCLecturaContextoEE.F14 = true;
            if (cblistjornadas5.Checked)
                objCLecturaContextoEE.F15 = true;

            if (cblistTipo1.Checked)
                objCLecturaContextoEE.C_1 = true;
            if (cblistTipo2.Checked)
                objCLecturaContextoEE.C_2 = true;
            if (cblistTipo3.Checked)
                objCLecturaContextoEE.C_3 = true;
            if (cblistTipo4.Checked)
                objCLecturaContextoEE.C_4 = true;

            if (rbtnRural.Checked)
                objCLecturaContextoEE._1_2_Rural = true;
            if (rbtnUrbana.Checked)
                objCLecturaContextoEE._1_2_Urbana = true;

            if (cblistTipo1.Checked)
                objCLecturaContextoEE._1_2_c1 = true;
            if (cblistTipo2.Checked)
                objCLecturaContextoEE._1_2_c2 = true;
            if (cblistTipo3.Checked)
                objCLecturaContextoEE._1_2_c3 = true;
            if (cblistTipo4.Checked)
                objCLecturaContextoEE._1_2_c4 = true;

            objCLecturaContextoEE._2_1_ = Convert.ToInt32(txt21.Text);

            objCLecturaContextoEE._2_2_E1_ = Convert.ToInt32(txtE1.Text);
            objCLecturaContextoEE._2_2_E2_ = Convert.ToInt32(txtE2.Text);
            objCLecturaContextoEE._2_2_E3_ = Convert.ToInt32(txtE3.Text);
            objCLecturaContextoEE._2_2_E4_ = Convert.ToInt32(txtE4.Text);
            objCLecturaContextoEE._2_2_E5_ = Convert.ToInt32(txtE5.Text);
            objCLecturaContextoEE._2_2_E6_ = Convert.ToInt32(txtE6.Text);

            objCLecturaContextoEE._2_3_S1_ = Convert.ToInt32(txtS1.Text);
            objCLecturaContextoEE._2_3_S2_ = Convert.ToInt32(txtS2.Text);
            objCLecturaContextoEE._2_3_S3_ = Convert.ToInt32(txtS3.Text);
            objCLecturaContextoEE._2_3_S4_ = Convert.ToInt32(txtS4.Text);
            objCLecturaContextoEE._2_3_SNosabe_ = Convert.ToInt32(txtNoSabe.Text);
            objCLecturaContextoEE._2_3_SNoTiene_ = Convert.ToInt32(txtNotiene.Text);

            if (rbtnSi24.Checked)
                objCLecturaContextoEE._2_4_Si = true;
            else if (rbtnNo24.Checked)
                objCLecturaContextoEE._2_4_No = true;
            else
            {
                objCLecturaContextoEE._2_4_Si = false;
                objCLecturaContextoEE._2_4_No = false;
            }

            objCLecturaContextoEE._2_5_1_ = Convert.ToInt32(txt25_1.Text);
            objCLecturaContextoEE.__2_5_2_ = Convert.ToInt32(txt25_2.Text);
            objCLecturaContextoEE._2_5_3_ = Convert.ToInt32(txt25_3.Text);

            objCLecturaContextoEE._3_1_ = txt31.Text;
            objCLecturaContextoEE._3_2_ = txt32.Text;
            objCLecturaContextoEE._3_3 = txt33.Text;

            if (rbtnSi34.Checked)
                objCLecturaContextoEE._3_4_Si = true;
            else if (rbtnNo34.Checked)
                objCLecturaContextoEE._3_4_No = true;
            else
            {
                objCLecturaContextoEE._3_4_Si = false;
                objCLecturaContextoEE._3_4_No = false;
            }

            objCLecturaContextoEE._3_4_1_ = txt341.Text;

            if (rbtnSi35.Checked)
                objCLecturaContextoEE._3_5_Si = true;
            else if (rbtnNo35.Checked)
                objCLecturaContextoEE._3_5_No = true;
            else
            {
                objCLecturaContextoEE._3_5_Si = false;
                objCLecturaContextoEE._3_5_No = false;
            }

            objCLecturaContextoEE._3_5_1_ = txt351.Text;
            objCLecturaContextoEE._3_5_2_ = txt352.Text;
            objCLecturaContextoEE._3_5_3_ = txt353.Text;
            objCLecturaContextoEE._3_5_4_ = txt354.Text;
            objCLecturaContextoEE._3_5_5_ = txt355.Text;
            objCLecturaContextoEE._3_5_6_ = txt356.Text;
            objCLecturaContextoEE._3_5_7_ = txt357.Text;
            objCLecturaContextoEE._3_6_ = txt36.Text;
            objCLecturaContextoEE._3_7_ = txt37.Text;

            if (rbtnSi38.Checked)
                objCLecturaContextoEE._3_8_Si = true;
            else if (rbtnNo38.Checked)
                objCLecturaContextoEE._3_8_No = true;
            else
            {
                objCLecturaContextoEE._3_8_Si = false;
                objCLecturaContextoEE._3_8_No = false;
            }

            objCLecturaContextoEE._3_8_1 = txt381.Text;

            if (rbtnSi39.Checked)
                objCLecturaContextoEE._3_9_Si = true;
            else if (rbtnNo39.Checked)
                objCLecturaContextoEE._3_9_No = true;
            else
            {
                objCLecturaContextoEE._3_9_Si = false;
                objCLecturaContextoEE._3_9_No = false;
            }

            objCLecturaContextoEE._3_9_1_ = txt391.Text;

            if (rbtnSi41.Checked)
                objCLecturaContextoEE._4_1_Si = true;
            else if (rbtnNo41.Checked)
                objCLecturaContextoEE._4_1_No = true;
            else if (rbtnAlgunnas41.Checked)
                objCLecturaContextoEE._4_1_Algunas = true;
            else
            {
                objCLecturaContextoEE._4_1_Si = false;
                objCLecturaContextoEE._4_1_No = false;
                objCLecturaContextoEE._4_1_Algunas = false;
            }

            objCLecturaContextoEE._4_2_ = Convert.ToInt32(txt42.Text);

            if (rbtnSi43.Checked)
                objCLecturaContextoEE._4_3_Si = true;
            else if (rbtnNo43.Checked)
                objCLecturaContextoEE._4_3_No = true;
            else
            {
                objCLecturaContextoEE._4_3_Si = false;
                objCLecturaContextoEE._4_3_No = false;
            }

            objCLecturaContextoEE._4_3_1_ = txt431.Text;
            objCLecturaContextoEE.Observaciones = txtObservaciones.Text;

            if (rbtnSi51.Checked)
                objCLecturaContextoEE._5_1_Si = true;
            else if (rbtnNo51.Checked)
                objCLecturaContextoEE._5_1_No = true;

            objCLecturaContextoEE._5_1_1 = Convert.ToInt32(txt511.Text);

            #endregion
            if (_medid == 0)
            {
                if (objCLecturaContextoEE.Almacenar(_eeid, _medid))
                {
                    MessageBox.Show("El proceso de almacenamiento finalizó con éxito.", "Información ESM", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("El proceso de almacenamiento finalizó sin éxito.", "Información ESM", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (objCLecturaContextoEE.Actualizar(_eeid, _medid))
                    MessageBox.Show("El proceso de actualización finalizó correctamente.", "Información ESM", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show("El proceso de actualización finalizó sin éxito.", "Información ESM", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            Almacenar(_eeid, _medid);
        }

        protected void CargarLCEE(int ieid)
        {

            #region Cargar Controles
            var lc = objCLecturaContextoEE.ObtenerLCEE(ieid);

            if (lc != null)
            {
                txtSedes.Text = lc.NumeroSedes.ToString();

                if ((bool)lc.f11)
                    cblistjornadas1.Checked = true;
                if ((bool)lc.f12)
                    cblistjornadas2.Checked = true;
                if ((bool)lc.f13)
                    cblistjornadas3.Checked = true;
                if ((bool)lc.f14)
                    cblistjornadas4.Checked = true;
                if ((bool)lc.f15)
                    cblistjornadas5.Checked = true;

                if ((bool)lc.C_1)
                    cblistTipo1.Checked = true;
                if ((bool)lc.C_2)
                    cblistTipo2.Checked = true;
                if ((bool)lc.C_3)
                    cblistTipo3.Checked = true;
                if ((bool)lc.C_4)
                    cblistTipo4.Checked = true;

                if ((bool)lc._1_2bRural)
                    rbtnRural.Checked = true;
                else if ((bool)lc._1_2bUrbana)
                    rbtnUrbana.Checked = true;

                txt21.Text = lc._2_1.ToString();

                txtE1.Text = lc._2_2_E1.ToString();
                txtE2.Text = lc._2_2_E2.ToString();
                txtE3.Text = lc._2_2_E3.ToString();
                txtE4.Text = lc._2_2_E4.ToString();
                txtE5.Text = lc._2_2_E5.ToString();
                txtE6.Text = lc._2_2_E6.ToString();

                txtS1.Text = lc._2_3_S1.ToString();
                txtS2.Text = lc._2_3_S2.ToString();
                txtS3.Text = lc._2_3_S3.ToString();
                txtS4.Text = lc._2_3_S4.ToString();
                txtNoSabe.Text = lc._2_3_NoSabe.ToString();
                txtNotiene.Text = lc._2_3_NoTiene.ToString();

                if ((bool)lc._2_4_Si)
                {
                    rbtnSi24.Checked = true;
                    rbtnNo24.Checked = false;

                    txt25_1.Enabled = true;
                    txt25_2.Enabled = true;
                    txt25_3.Enabled = true;
                }
                else if ((bool)lc._2_4_No)
                {
                    rbtnSi24.Checked = false;
                    rbtnNo24.Checked = true;
                }

                txt25_1.Text = lc._2_5_1.ToString();
                txt25_2.Text = lc._2_5_2.ToString();
                txt25_3.Text = lc._2_5_3.ToString();

                txt31.Text = lc._3_1.ToString();
                txt32.Text = lc._3_2.ToString();
                txt33.Text = lc._3_3.ToString();

                if ((bool)lc._3_4_Si)
                {
                    rbtnSi34.Checked = true;
                    rbtnNo34.Checked = false;
                    txt341.Enabled = true;
                }
                else if ((bool)lc._3_4_No)
                {
                    rbtnNo34.Checked = true;
                    rbtnSi34.Checked = false;

                }

                txt341.Text = lc._3_4_1.ToString();

                if ((bool)lc._3_5_Si)
                {
                    rbtnSi35.Checked = true;
                    rbtnNo35.Checked = false;

                    txt351.Enabled = true;
                    txt352.Enabled = true;
                    txt353.Enabled = true;
                    txt354.Enabled = true;
                    txt355.Enabled = true;
                    txt356.Enabled = true;
                    txt357.Enabled = true;
                    txt36.Enabled = true;
                    txt37.Enabled = true;
                    rbtnNo38.Enabled = true;
                    rbtnSi38.Enabled = true;
                    txt381.Enabled = true;
                    rbtnNo39.Enabled = true;
                    rbtnSi39.Enabled = true;
                    txt391.Enabled = true;
                }
                else if ((bool)lc._3_5_No)
                {
                    rbtnNo35.Checked = true;
                    rbtnSi35.Checked = false;
                }

                txt351.Text = lc._3_5_1.ToString();
                txt352.Text = lc._3_5_2.ToString();
                txt353.Text = lc._3_5_3.ToString();
                txt354.Text = lc._3_5_4.ToString();
                txt355.Text = lc._3_5_5.ToString();
                txt356.Text = lc._3_5_6.ToString();
                txt357.Text = lc._3_5_7.ToString();
                txt36.Text = lc._3_6.ToString();
                txt37.Text = lc._3_7.ToString();

                if ((bool)lc._3_8_Si)
                {
                    rbtnSi38.Checked = true;
                    rbtnNo38.Checked = false;
                    txt381.Enabled = true;
                }
                else if ((bool)lc._3_8_No)
                {
                    rbtnNo38.Checked = true;
                    rbtnSi38.Checked = false;
                }

                txt381.Text = lc._3_8_1.ToString();

                if ((bool)lc._3_9_Si)
                {
                    rbtnSi39.Checked = true;
                    rbtnNo39.Checked = false;
                    txt391.Enabled = true;
                }
                else if ((bool)lc._3_9_No)
                {
                    rbtnNo39.Checked = true;
                    rbtnSi39.Checked = false;
                }

                txt391.Text = lc._3_9_1.ToString();

                if ((bool)lc._4_1_Si)
                {
                    rbtnSi41.Checked = true;
                    txt42.Enabled = true;
                }
                else if ((bool)lc._4_1_No)
                    rbtnNo41.Checked = true;
                else if ((bool)lc._4_1_Algunas)
                {
                    rbtnAlgunnas41.Checked = true;
                    txt42.Enabled = true;
                }

                txt42.Text = lc._4_2.ToString();

                if ((bool)lc._4_3_Si)
                {
                    rbtnSi43.Checked = true;
                    rbtnNo43.Checked = false;
                    txt431.Enabled = true;
                }
                else if ((bool)lc._4_3_No)
                    rbtnNo43.Checked = true;

                txt431.Text = lc._4_3_1.ToString();

                if ((bool)lc._5_1_Si)
                {
                    rbtnSi51.Checked = true;
                    rbtnNo51.Checked = false;
                    txt511.Enabled = true;
                }
                else if ((bool)lc._5_1_No)
                {
                    rbtnNo51.Checked = true;
                    rbtnSi51.Checked = false;
                }

                txt511.Text = lc._5_1_1.ToString();

                txtObservaciones.Text = lc.Observaciones.ToString();
            }
            #endregion
        }

    }
}
